﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Trading.Purchase.GoodsPurchaseItem;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Trading.Purchase.GoodsPurchaseItem;

namespace BCTMSRESTful.Controllers.Management.Trading.Purchase
{
    /// <summary>
    /// 商品采购项
    /// </summary>
    [ApiExplorerSettings(GroupName = "AdminTrading")]
    [Route("api/v1/sim/[controller]")]
    public class GoodsPurchaseItemController : ManagementBaseController
    {
        private readonly IGoodsPurchaseItemService goodsPurchaseItemService;

        public GoodsPurchaseItemController(IGoodsPurchaseItemService goodsPurchaseItemService)
        {
            this.goodsPurchaseItemService = goodsPurchaseItemService;
        }

        #region 添加商品采购项
        /// <summary>
        ///  添加商品采购项
        /// </summary>
        /// <param name="requestDto">商品采购项信息</param>
        /// <response code="201">添加商品采购项成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加商品采购项")]
        [ProducesResponseType(typeof(GoodsPurchaseItemDto), 201)]
        public IActionResult Post(
            [FromBody]GoodsPurchaseItemRequestDto requestDto)
        {
            var result = this.goodsPurchaseItemService.Add(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改商品采购项
        /// <summary>
        ///  修改商品采购项
        /// </summary>
        /// <param name="goodsPurchaseItemId">商品采购项Id</param>
        /// <param name="requestDto">商品采购项信息</param>
        /// <response code="200">修改商品采购项成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{goods_purchase_item_id}")]
        [Log("修改商品采购项")]
        [ProducesResponseType(typeof(GoodsPurchaseItemDto), 200)]
        public IActionResult Put([FromRoute(Name = "goods_purchase_item_id")]long goodsPurchaseItemId, [FromBody]GoodsPurchaseItemRequestDto requestDto)
        {
            GoodsPurchaseItemPutDto putDto = requestDto.As<GoodsPurchaseItemPutDto>();
            putDto.GoodsPurchaseItemId = goodsPurchaseItemId;
            return StatusCode((int)HttpStatusCode.OK, this.goodsPurchaseItemService.Update(putDto));
        }
        #endregion

        #region 删除商品采购项
        /// <summary>
        ///  删除商品采购项
        /// </summary>
        /// <param name="goodsPurchaseItemId">商品采购项Id</param>
        /// <response code="204">删除商品采购项成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{goods_purchase_item_id}")]
        [Log("删除商品采购项")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "goods_purchase_item_id")]long goodsPurchaseItemId)
        {
            this.goodsPurchaseItemService.Delete(goodsPurchaseItemId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取商品采购项
        /// <summary>
        ///  获取商品采购项
        /// </summary>
        /// <param name="goodsPurchaseItemId">商品采购项Id</param>
        /// <response code="200">获取商品采购项</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{goods_purchase_item_id}")]
        [ProducesResponseType(typeof(GoodsPurchaseItemDto), 200)]
        public IActionResult Get([FromRoute(Name = "goods_purchase_item_id")]long goodsPurchaseItemId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.goodsPurchaseItemService.Get(goodsPurchaseItemId));
        }
        #endregion

        #region 获取商品采购项列表(采购员查看自己发起的)
        /// <summary>
        ///  获取商品采购项列表(采购员查看自己发起的)
        /// </summary>
        /// <param name="hasTask">已生成采购任务</param>
        /// <response code="200">获取商品采购项列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(GoodsPurchaseItemDto), 200)]
        public IActionResult Get(
            [FromQuery(Name = "has_task")]bool? hasTask) 
        {
            return StatusCode((int)HttpStatusCode.OK, this.goodsPurchaseItemService.GetList(this.Admin.AdminId, hasTask));
        }
        #endregion

        #region 获取商品采购项（分页）
        /// <summary>
        ///  获取商品采购项（分页）
        /// </summary>
        /// <param name="categoryName">商品分类名称</param>
        /// <param name="goodsName">商品名称</param>
        /// <param name="sku">商品SKU</param>
        /// <param name="vendorName">供应商名称</param>
        /// <param name="adminName">创建人姓名</param>
        /// <param name="vendorId">供应商Id</param>
        /// <param name="hasTask">已生成采购任务</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取商品采购项（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<GoodsPurchaseItemDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "category_name")]string categoryName,
            [FromQuery(Name = "goods_name")]string goodsName,
            [FromQuery(Name = "sku")]string sku,
            [FromQuery(Name = "vendor_name")]string vendorName,
            [FromQuery(Name = "full_name")]string adminName,
            [FromQuery(Name = "vendor_id")]int? vendorId,
            [FromQuery(Name = "has_task")]bool? hasTask,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.goodsPurchaseItemService.Query(categoryName, goodsName, sku, vendorName, adminName, null, vendorId, hasTask, start, end, sortName, ascending, index, size));
        }
        #endregion

        #region 获取采购项存在的供应商列表
        /// <summary>
        ///  获取采购项存在的供应商列表
        /// </summary>
        /// <response code="200">获取采购项存在的供应商列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("Vendor")]
        [ProducesResponseType(typeof(GoodsPurchaseItemDto), 200)]
        public IActionResult GetPurchaseItemsVendorList()
        {
            return StatusCode((int)HttpStatusCode.OK, this.goodsPurchaseItemService.GetPurchaseItemsVendorList());
        }
        #endregion
        
    }
}

